# 开发环境启动脚本 (PowerShell)
# DB-GPT Local Development Startup Script

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  DB-GPT 本地开发环境启动助手" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# 检查后端服务是否运行
Write-Host "1. 检查后端服务状态..." -ForegroundColor Yellow
$backendRunning = $false
try {
    $response = Invoke-WebRequest -Uri "http://127.0.0.1:5670" -Method HEAD -TimeoutSec 2 -ErrorAction Stop
    $backendRunning = $true
    Write-Host "   ✓ 后端服务正在运行 (5670端口)" -ForegroundColor Green
} catch {
    Write-Host "   ✗ 后端服务未运行" -ForegroundColor Red
    Write-Host "   请先启动后端服务: dbgpt start webserver --config configs/dbgpt-proxy-siliconflow.toml" -ForegroundColor Yellow
}

Write-Host ""

# 检查环境配置
Write-Host "2. 检查前端环境配置..." -ForegroundColor Yellow
if (Test-Path ".env") {
    Write-Host "   ✓ .env 配置文件存在" -ForegroundColor Green
    $envContent = Get-Content .env | Select-String "API_BASE_URL"
    if ($envContent) {
        Write-Host "   ✓ API地址配置: $envContent" -ForegroundColor Green
    }
} else {
    Write-Host "   ✗ .env 配置文件不存在" -ForegroundColor Red
    Write-Host "   正在创建默认配置..." -ForegroundColor Yellow
    @"
# DB-GPT Local Development Environment Configuration
API_BASE_URL=http://127.0.0.1:5670
"@ | Out-File -FilePath ".env" -Encoding UTF8
    Write-Host "   ✓ 已创建 .env 配置文件" -ForegroundColor Green
}

Write-Host ""

# 检查依赖
Write-Host "3. 检查依赖安装..." -ForegroundColor Yellow
if (Test-Path "node_modules") {
    Write-Host "   ✓ 依赖已安装" -ForegroundColor Green
} else {
    Write-Host "   ✗ 依赖未安装" -ForegroundColor Red
    Write-Host "   正在安装依赖..." -ForegroundColor Yellow
    npm install
}

Write-Host ""

# 启动开发服务器
Write-Host "4. 启动前端开发服务器..." -ForegroundColor Yellow
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  开发服务器信息" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  前端开发: http://localhost:3000" -ForegroundColor Green
Write-Host "  登录页面: http://localhost:3000/login" -ForegroundColor Green
Write-Host "  后端API:  http://127.0.0.1:5670" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "按 Ctrl+C 停止开发服务器" -ForegroundColor Yellow
Write-Host ""

npm run dev
